package cn.didi.feidi.javabackend.server.web.controller;

import cn.didi.feidi.javabackend.mysql.jpa.entity.Userxx;
import cn.didi.feidi.javabackend.mysql.jpa.exception.MyException;
import cn.didi.feidi.javabackend.mysql.jpa.response.ApiResponse;
import cn.didi.feidi.javabackend.mysql.jpa.user.User;
import cn.didi.feidi.javabackend.server.user.service.UserService;
import cn.didi.feidi.javabackend.server.web.constantErrorMes.ErrorMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2017/7/7.
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    private static int counter = 0;

    /**
     * 例子，访问http://localhost:8080/userol/example
     * @return {"id":2,"name":"xiaoming","createdate":1499345500000,"roles":[{"id":2,"name":"admin"}]}
     */

    @RequestMapping("/getUserById")
    @ResponseBody
    public ApiResponse<User> getUserById(@RequestParam String id) throws MyException {
        User user = userService.getUserById(id);
        if(user == null)
            throw new MyException(ErrorMessage.USER_NOT_EXISTED);
        ApiResponse<User> response = new ApiResponse<User>("0", "success");
        response.setObj(user);
        return response;
    }

    @RequestMapping("/registerUser")
    @ResponseBody
    public ApiResponse<String> registerUser(User user) throws MyException {
        validate(user);
        if(userService.getUserById(user.getUserId()) != null)
            throw new MyException(ErrorMessage.USER_IS_EXISTED);
        addUser(user);
        ApiResponse<String> response = new ApiResponse<String>("0", "success");
        return response;

    }

    @RequestMapping("/addUser")
    @ResponseBody
    public ApiResponse<String> addUser(User user) throws MyException {
        if(user == null){
           user = new User();
           user.setUserId(Integer.toString(++counter));
           user.setName("Tom_"+counter);
           user.setPhoneNumber("123456");
           user.setGrade(90.8f);
           user.setPortraitUrl("/imgs/UUID.jpg");
        }
        validate(user);
        userService.addUser(user);
        ApiResponse<String> response = new ApiResponse<String>("0", "success");
        return response;
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public ApiResponse<String> updateUser(User user) throws MyException {
        validate(user);
        userService.updateUser(user);
        ApiResponse<String> response = new ApiResponse<String>("0", "success");
        return response;
    }

    @RequestMapping("/deleteUserById")
    @ResponseBody
    public ApiResponse<String> deleteUserById(String id){
        userService.deleteUserById(id);
        ApiResponse<String> response = new ApiResponse<String>("0", "success");
        return response;
    }

    @RequestMapping("/getUsersByName")
    @ResponseBody
    public ApiResponse<User> getUsersByName(String name){
        List<User> users = userService.getUsersByName(name);
        ApiResponse<User> response = new ApiResponse<User>("0", "success");
        response.setObjList(users);
        return response;
    }

    @RequestMapping("/updateUserById")
    @ResponseBody
    public ApiResponse<String> updateUserById(String id){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", "Tom");
        params.put("grade", 60.0F);
        userService.updateUserById(id, params);
        ApiResponse<String> response = new ApiResponse<>("0", "success");
        return response;
    }

    private void validate(User user) throws MyException{
        StringBuilder errorMessage = new StringBuilder();
        if(user.getUserId() == null)
            errorMessage.append(ErrorMessage.USER_WITHOUT_ID);
        if(user.getName() == null)
            errorMessage.append(ErrorMessage.USER_WITHOUT_NAME);
        if(user.getPhoneNumber() == null)
            errorMessage.append(ErrorMessage.USER_WITHOUT_PHONENUMBER);
        if(errorMessage.length() != 0)
            throw new MyException(errorMessage.toString());
    }

}
